(define (calendar title content)
  (let ((post-url (<java.net.URL> "http://www.google.com/calendar/feeds/xxx@gmail.com/private/full"))
        (my-service (<com.google.gdata.client.GoogleService> "cl" "exampleCo-exampleApp-1")))
    (my-service:setUserCredentials "xxx@gmail.com" "xxx")
    (let ((ext-profile (my-service:getExtensionProfile))
          (event-feed (<com.google.gdata.data.extensions.EventFeed>)))
      (event-feed:declareExtensions ext-profile)
      (let ((my-feed (my-service:getFeed post-url <com.google.gdata.data.Feed>))
            (my-entry (<com.google.gdata.data.extensions.EventEntry>)))
        (my-entry:setTitle (<com.google.gdata.data.PlainTextConstruct> title))
        (my-entry:setContent (<com.google.gdata.data.PlainTextConstruct> content))
        (let ((author (<com.google.gdata.data.Person> "xxx" #!null "xxx@gmail.com")))
          ((my-entry:getAuthors):add author)
          (let ((start-time (com.google.gdata.data.DateTime:now))
                (end-time (com.google.gdata.data.DateTime:now))
                (event-times (com.google.gdata.data.extensions.When)))
            (event-times:setStartTime start-time)
            (event-times:setEndTime end-time)
            (my-entry:addTime event-times)
            (my-service:insert post-url my-entry)))))))

(define (get-spreadsheet service spreadsheet-name)
  (let* ((factory (com.google.gdata.client.spreadsheet.FeedURLFactory:getDefault))
         (feed (service:getFeed (factory:getSpreadsheetsFeedUrl) <com.google.gdata.data.spreadsheet.SpreadsheetFeed>))
         (spreadsheets (feed:getEntries))
         (iterator (spreadsheets:iterator)))
    (letrec ((next (lambda ()
                     (if (iterator:hasNext)
                       (let ((spreadsheet (iterator:next)))
                         (if (string=? spreadsheet-name ((spreadsheet:getTitle):getPlainText))
                            spreadsheet
                            (next)))
                       (throw (<java.lang.Exception> (string-append "spreadsheet:\"" spreadsheet-name "\" not found")))))))
      (next))))

(define (get-worksheet service spreadsheet-name worksheet-name)
  (let* ((spreadsheet (get-spreadsheet service spreadsheet-name))
         (worksheets (spreadsheet:getWorksheets))
         (iterator (worksheets:iterator)))
    (letrec ((next (lambda ()
                     (if (iterator:hasNext)
                       (let ((worksheet (iterator:next)))
                         (if (string=? worksheet-name ((worksheet:getTitle):getPlainText))
                            worksheet
                            (next)))
                       (throw (<java.lang.Exception> (string-append "worksheet:\"" worksheetsheet-name "\" not found")))))))
      (next))))

(define (base-write service worksheet var-map-list)
  (let* ((list-feed-url (worksheet:getListFeedUrl))
         (new-entry (<com.google.gdata.data.spreadsheet.ListEntry>))
         (cell-feed (service:getFeed (worksheet:getCellFeedUrl) <com.google.gdata.data.spreadsheet.CellFeed>))
         (cells (cell-feed:getEntries))
         (elements (new-entry:getCustomElements))
         (iterator (cells:iterator)))
    (letrec ((next (lambda (flag)
                     (if (iterator:hasNext)
                       (let* ((cell ((iterator:next):getCell))
                              (value (cell:getValue))
                              (row (cell:getRow)))
                         (if (and (> (string-length value) 0) (= row 1))
                           (begin
;                             (newline)
;                             (display "write ")
;                             (display value)
                             (elements:setValueLocal value (read-mem value))
                             (next #t))
                           (next flag)))
                       flag))))
      (if (next #f)
        (service:insert list-feed-url new-entry)))))
(define (base-read service worksheet var-map-list time)
  (let* ((time-stamp ((java.util.Calendar:getInstance):getTime))
         (list-feed-url (worksheet:getListFeedUrl))
         (cell-feed (service:getFeed (worksheet:getCellFeedUrl) <com.google.gdata.data.spreadsheet.CellFeed>))
         (cells (cell-feed:getEntries))
         (iterator (cells:iterator)))
    (letrec ((next (lambda (cols)
                     (if (iterator:hasNext)
                        (let* ((cell ((iterator:next):getCell))
                               (value (cell:getValue))
                               (column (cell:getCol))
                               (row (cell:getRow)))
                           (if (and (> (string-length value) 0) (= row 1))
                             (next (append cols (list value)))
                             (if (and (> (string-length value) 0) (= row (+ time 2)))
                               (let* ((col-name (list-ref cols (- column 1)))
                                      (sym-dat (get-sym var-map-list col-name)))
                                 (write-mem col-name (string->number value))
;                                 ((java.lang.System:.out):println "jiffy")
;                                 (newline)
;                                 (display "write ")
;                                 (display (car sym-dat))
;                                 (display (string-append " byte" (if (= (car sym-dat) 1) "" "s") " ("))
;                                 (display value)
;                                 (display ") from column ")
;                                 (display col-name)
;                                 (display " to address ")
;                                 (display (cadr sym-dat))
                                 (next cols))
                               (next cols))))))))
      (next (list))))
;  (newline)
)

(define (base-init service worksheet column data-list)
  (let ((list-feed-url (worksheet:getListFeedUrl)))
    (letrec ((next (lambda (l)
                     (if (> (length l) 0)
                       (let* ((new-entry (<com.google.gdata.data.spreadsheet.ListEntry>))
                              (elements (new-entry:getCustomElements)))
                         (elements:setValueLocal column (car l))
                         (service:insert list-feed-url new-entry)
                         (next (cdr l)))))))
      (next data-list))))

(define (service-connect account password)
  (let ((service (<com.google.gdata.client.spreadsheet.SpreadsheetService> "jiffy demo")))
    (service:setUserCredentials "xxx@gmail.com" "xxx")
    service))


